//
//  MyCollectTableViewController.m
//  ZLExamination
//
//  Created by Lei_Zhong_Xun on 2017/8/21.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "MyCollectTableViewController.h"
#import "MyCollectTableViewCell.h"
#import "MyCollectModel.h"

#import "ZLExamResultModel.h"

#import "ZLWrongParsingViewController.h"

@interface MyCollectTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation MyCollectTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    [self.myTableView registerClass:[MyCollectTableViewCell class] forCellReuseIdentifier:MyCollectTableViewCellID];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - load data
- (void)loadData {
    
    [[YCRequestDataService sharedManager] Post:@"user/collection/lists" parameters:nil success:^(id response) {
        if (response[@"error_msg"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            
            _dataArr = [NSMutableArray new];
            
            NSArray *lists = response[@"response_data"][@"lists"];
            [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                MyCollectModel *model = [[MyCollectModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
        
            [self.myTableView reloadData];
        }
        
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
    
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectTableViewCell *myCollectCell = [tableView dequeueReusableCellWithIdentifier:MyCollectTableViewCellID forIndexPath:indexPath];
    
    myCollectCell.model = _dataArr[indexPath.row];
    
    return myCollectCell;
}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 41;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

//tableview左面线不能顶到头的问题。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.userInteractionEnabled = NO;
    
    MyCollectModel *model = _dataArr[indexPath.row];
    
    NSLog(@"curriculum_id== %@",model.curriculum_id);
    
    [[YCRequestDataService sharedManager] Post:@"user/collection/info" parameters:@{@"curriculum_id":model.curriculum_id} success:^(id response) {
        tableView.userInteractionEnabled = YES;
        
        if (response[@"error_msg"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            
            NSArray *lists = response[@"response_data"][@"lists"];
            
            NSMutableArray *singleModels = [NSMutableArray new];
            NSMutableArray *multipleModels = [NSMutableArray new];
                        
            [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZLExamResultListModel *model = [[ZLExamResultListModel alloc] initWithDataDic:obj];
                model.questionNumber = idx+1;
                if (model.type.integerValue == 1) {
                    //单选
                    [singleModels addObject:model];
                } else {
                    //多选
                    [multipleModels addObject:model];
                }
            }];
            
#pragma mark - 跳转详情
            ZLWrongParsingViewController *wpVC = [[ZLWrongParsingViewController alloc] init];
            wpVC.answersDataArr = @[singleModels,multipleModels];
            wpVC.curriculum_id = model.curriculum_id;
            wpVC.navigationItem.title = model.curriculum_name;
            [self.navigationController pushViewController:wpVC animated:YES];
        }
        
    } failure:^{
        tableView.userInteractionEnabled = YES;
        NSLog(@"网络或服务器出错");
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
